from fastapi import APIRouter, Depends
import db.crud as dbutils
from utils import utils
from db.database import get_db
from sqlalchemy.orm import Session
from domain.response import success, error
from typing import Dict, Coroutine, Any
from utils.utils import version2VersionVOList, message2messageVOWrapper

# 路由层一般用于接收请求，处理请求中携带的一系列参数
# 处理请求

router = APIRouter(
    prefix="/app1",
    tags=["app1"],
    dependencies=[],
    responses={404: {"description": "app1 error"}}
)


@router.get("/path/parameters")
async def param():
    print("param")


@router.get("/compare")
async def compare():
    print("compare")


# 测试数据：bid填入test
@router.get("/getVersion/{bid}")
async def getVersion(bid: str, db: Session = Depends(get_db)):
    if bid is None or bid == "":
        return error(40001, "参数错误")
    if bid == "han_xu_bid":
        return None
    version = await dbutils.get_version(bid, db)
    return await success(await version2VersionVOList(version))


@router.get("/getVersionMessage")
async def get_version_message(version: str, db: Session = Depends(get_db)):
    if version is None or version == "":
        return await error(40001, "参数不可为空")
    version_code = await utils.version2int(version)
    messages = await dbutils.get_version_msg(version_code, db)
    result = []
    for i in messages:
        result.append(await message2messageVOWrapper(i))
    return await success(result)


@router.get("/responseVOTest")
async def response(bid: str, db: Session = Depends(get_db)):
    version = await dbutils.get_version(bid, db)
    b = await utils.Version2UpdateVOWrapper(version)
    return await success(b)

